package com.ctg.payroll2.action.social;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.bean.filter.social.CsSocialFilter;
import com.ctg.payroll2.bean.filter.social.PbSocialMasterFilter;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.bean.po.social.CsSocial;
import com.ctg.payroll2.bean.po.social.CsSocialDetail;
import com.ctg.payroll2.bean.po.social.CsTemplateSocialItemSearchBean;
import com.ctg.payroll2.service.social.CsSocialService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 客户社保模板管理
 * @author 鲁力 2009-9-1 下午06:10:28
 *
 */
public class CsSocialAction extends ActionSupport {
	private static final long serialVersionUID = 5523283522981869682L;
	/**
	 * 查询翻页结果
	 */
	private Page result;
	/**
	 * 社保翻页结果
	 */
	private Page socialResult;
	/**
	 * 查询条件过滤器
	 */
	private CsSocialFilter filter;
	/**
	 * 社保项目查询条件过滤器
	 */
	private PbSocialMasterFilter socialFilter;
	private CsSocialDetail details;
	/**
	 * 新增和更新时用到的
	 */
	private CsSocial csSocial;
	/**
	 * 用到的service,用spring注入
	 */
	private CsSocialService service;
	private String templateIds;
	private String socialSecurityIds;
	private Long socialSecurityId;
	private List<CsTemplateSocialItemSearchBean> beanList;
	/**
	 * 显示社保列表用到的翻页action
	 * @return
	 */
	/*public String querySocial(){
		HttpServletRequest request = ServletActionContext.getRequest();
		if(socialFilter == null){
			socialFilter = new PbSocialMasterFilter();
		}
		socialFilter.setStartIndex(Parse.toInteger(request.getParameter("start") , 0));
		socialFilter.setEndIndex(Parse.toInteger(request.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE));
		socialResult = service.querySocialItemAll(socialFilter);
		return SUCCESS;
	}*/
	/**
	 * 页面翻页查询 used
	 * @return 翻页结果
	 */
	public String query(){
		SysUser user = this.getCurrentUser();
		HttpServletRequest request = ServletActionContext.getRequest();
		if(filter == null){
			filter = new CsSocialFilter();
		}
		filter.setStartIndex(Parse.toInteger(request.getParameter("start") , 0));
		filter.setEndIndex(Parse.toInteger(request.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE));
		filter.setCompanyCode(user.getCompanyCode());
		result = service.queryCsSocial(filter);
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String queryCsTemplateDetailByFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String templateId = request.getParameter("templateId");
		beanList = service.getPbSocialItemByFilter(templateId);
		
		return SUCCESS;
	}
	/**
	 * 新增一个推荐社保模板
	 * @return
	 */
	public String insertCsSocial() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String adds = request.getParameter("adds");
		List<CsTemplateSocialItemSearchBean> addsList = new ArrayList<CsTemplateSocialItemSearchBean>();
		JSONArray modifiedArray = JSONArray.fromObject(adds);
		for (int i = 0; i < modifiedArray.size(); i++) {
			Object o = modifiedArray.get(i);
			JSONObject jsonObject = JSONObject.fromObject(o);
			CsTemplateSocialItemSearchBean csSocialDetail = (CsTemplateSocialItemSearchBean) JSONObject
					.toBean(jsonObject, CsTemplateSocialItemSearchBean.class);
			addsList.add(csSocialDetail);
		}
		csSocial.setAddfieds(addsList);
		SysUser user = getCurrentUser();
		service.insertCsSocial(csSocial, user);
		return SUCCESS;
	}
	/**
	 * 还未完成
	 * @return
	 */
	public String updateCsSocial(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String adds = request.getParameter("adds");
		List<CsTemplateSocialItemSearchBean> addsList=new ArrayList<CsTemplateSocialItemSearchBean>();
		JSONArray addsArray = JSONArray.fromObject(adds);
		for (int i = 0; i < addsArray.size(); i++) {
			   Object o = addsArray.get(i);
			   JSONObject jsonObject = JSONObject.fromObject(o);
			   CsTemplateSocialItemSearchBean csSocialDetail = (CsTemplateSocialItemSearchBean) JSONObject.toBean(jsonObject, CsTemplateSocialItemSearchBean.class);
			   addsList.add(csSocialDetail);
			}

		String modifs = request.getParameter("modifs");
		List<CsTemplateSocialItemSearchBean> modifsList=new ArrayList<CsTemplateSocialItemSearchBean>();
		JSONArray modifiedArray = JSONArray.fromObject(modifs);
		for (int i = 0; i < modifiedArray.size(); i++) {
			   Object o = modifiedArray.get(i);
			   JSONObject jsonObject = JSONObject.fromObject(o);
			   CsTemplateSocialItemSearchBean csSocialDetail = (CsTemplateSocialItemSearchBean) JSONObject.toBean(jsonObject, CsTemplateSocialItemSearchBean.class);
			   modifsList.add(csSocialDetail);
			}
		
		String deleted = request.getParameter("deleted");
		List<CsTemplateSocialItemSearchBean> deletedsList=new ArrayList<CsTemplateSocialItemSearchBean>();
		JSONArray deletedArray = JSONArray.fromObject(deleted);
		for (int i = 0; i < deletedArray.size(); i++) {
		   Object o = deletedArray.get(i);
		   JSONObject jsonObject = JSONObject.fromObject(o);
		   CsTemplateSocialItemSearchBean csSocialDetail = (CsTemplateSocialItemSearchBean) JSONObject.toBean(jsonObject, CsTemplateSocialItemSearchBean.class);
		   deletedsList.add(csSocialDetail);
		}
		
		csSocial.setAddfieds(addsList);
		csSocial.setModifieds(modifsList);
		csSocial.setDeleteds(deletedsList);
		SysUser u = getCurrentUser();
		service.updateCsSocial(csSocial, u);
		return SUCCESS;
	}
	/**
	 * 删除推荐模板
	 * @return
	 */
	public String deleteCsSocialById(){
		try {
			service.deleteCsSocialByIds(csSocial.getIds());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String retrieveEntity(){
		csSocial = service.getCsSocialById(csSocial.getTemplate_id());
		return SUCCESS;
	}
	private SysUser getCurrentUser(){
		return (SysUser)ServletActionContext.getRequest().getSession().getAttribute(Constants.CURRENT_USER);
	}
	/**************************************************************
	 * 
	 * getter, setter方法
	 * 
	 *************************************************************/
	/**
	 * @return 返回csSocial
	 */
	public CsSocial getCsSocial() {
		return csSocial;
	}
	/**
	 * @param csSocial 设置csSocial
	 */
	public void setCsSocial(CsSocial csSocial) {
		this.csSocial = csSocial;
	}
	/**
	 * @return 返回details
	 */
	public CsSocialDetail getDetails() {
		return details;
	}
	/**
	 * @param details 设置details
	 */
	public void setDetails(CsSocialDetail details) {
		this.details = details;
	}
	/**
	 * @return 返回filter
	 */
	public CsSocialFilter getFilter() {
		return filter;
	}
	/**
	 * @param filter 设置filter
	 */
	public void setFilter(CsSocialFilter filter) {
		this.filter = filter;
	}
	/**
	 * @return 返回result
	 */
	public Page getResult() {
		return result;
	}
	/**
	 * @param result 设置result
	 */
	public void setResult(Page result) {
		this.result = result;
	}
	/**
	 * @return 返回socialFilter
	 */
	public PbSocialMasterFilter getSocialFilter() {
		return socialFilter;
	}
	/**
	 * @param socialFilter 设置socialFilter
	 */
	public void setSocialFilter(PbSocialMasterFilter socialFilter) {
		this.socialFilter = socialFilter;
	}
	/**
	 * @return 返回socialResult
	 */
	public Page getSocialResult() {
		return socialResult;
	}
	/**
	 * @param socialResult 设置socialResult
	 */
	public void setSocialResult(Page socialResult) {
		this.socialResult = socialResult;
	}
	/**
	 * @return 返回socialSecurityId
	 */
	public Long getSocialSecurityId() {
		return socialSecurityId;
	}
	/**
	 * @param socialSecurityId 设置socialSecurityId
	 */
	public void setSocialSecurityId(Long socialSecurityId) {
		this.socialSecurityId = socialSecurityId;
	}
	/**
	 * @return 返回socialSecurityIds
	 */
	public String getSocialSecurityIds() {
		return socialSecurityIds;
	}
	/**
	 * @param socialSecurityIds 设置socialSecurityIds
	 */
	public void setSocialSecurityIds(String socialSecurityIds) {
		this.socialSecurityIds = socialSecurityIds;
	}
	/**
	 * @return 返回templateIds
	 */
	public String getTemplateIds() {
		return templateIds;
	}
	/**
	 * @param templateIds 设置templateIds
	 */
	public void setTemplateIds(String templateIds) {
		this.templateIds = templateIds;
	}
	/**
	 * @param service 设置service
	 */
	public void setService(CsSocialService service) {
		this.service = service;
	}
	/**
	 * @return 返回beanList
	 */
	public List<CsTemplateSocialItemSearchBean> getBeanList() {
		return beanList;
	}
	/**
	 * @param beanList 设置beanList
	 */
	public void setBeanList(List<CsTemplateSocialItemSearchBean> beanList) {
		this.beanList = beanList;
	}
	
}
